package org.ece3574.WTParty;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;


public class MeActivity extends Activity {
	
	private WTParty wtParty;
	TextView tvName;
    TextView tvHometown;
    //ListView tvEvents;
    ImageView imgProfilePic;
    boolean isLoggedIn = false;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.me);
    	tvName = (TextView)this.findViewById(R.id.txtName);
        tvHometown = (TextView)this.findViewById(R.id.txtHometown);
        imgProfilePic = (ImageView)this.findViewById(R.id.imgProfilePic);
        
        wtParty = WTParty.getInstance(this);
        checkLogin();
    }
    
    private void checkLogin()
    {
    	if(!isLoggedIn)
    	{
	    	if(wtParty.isLoggedIn())
	    	{
	    		new ProfileInfoUpdater().execute((Void)null);
	    		isLoggedIn=true;
	    	}
	    	else
	    	{
	    		isLoggedIn=false;
	    	}
    	}
    }
    
    private class ProfileInfoUpdater extends AsyncTask<Void, Void, facebookProfile>
    {

		@Override
		protected facebookProfile doInBackground(Void... arg0) {
    		facebookProfile profile = wtParty.getProfile();
    		return profile;
		}
		
		protected void onPostExecute(facebookProfile profile)
		{
    		if(profile!=null)
    		{
    			tvName.setText(profile.name);
    			if(profile.hometown!=null)
    				tvHometown.setText("Hometown: " + profile.hometown);
    			if(profile.picture!=null)
    				imgProfilePic.setImageBitmap(profile.picture);
    		}
		}
    	
    }
    
    public void onResume()
    {
    	super.onResume();
    	checkLogin();
    }
    
    
}